package xin.printk.android.module.plan;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import java.util.List;

import xin.printk.android.data.db.dao.PlanDao;
import xin.printk.android.data.entity.Plan;
import xin.printk.android.utils.RequestCallback;

public class PlanModel {

    public static void getUndonePlans(final Context context, final int index, final RequestCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final List<Plan> list = PlanDao.getUndonePlans(context, index);
                if (callback != null) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            if (list == null) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess(list);
                            }
                        }
                    });
                }
            }
        }).start();
    }

    public static void getDonePlans(final Context context, final int index, final RequestCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final List<Plan> list = PlanDao.getCompletedPlans(context, index);
                if (callback != null) {
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            if (list == null) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess(list);
                            }
                        }
                    });
                }
            }
        }).start();
    }

    public static void addPlan(final Context context, final Plan plan, final RequestCallback callback) {
        if (plan.getTitle().isEmpty()) {
            if (callback != null) {
                callback.onFailure("title can not empty");
            }
            return;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                final long ret = PlanDao.addPlan(context, plan);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            if (ret == -1) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess("success");
                            }
                        }
                    }
                });
            }
        }).start();
    }

    public static void updatePlan(final Context context, final Plan plan, final RequestCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final int ret = PlanDao.updatePlan(context, plan);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            if (ret < 0) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess(plan);
                            }
                        }
                    }
                });
            }
        }).start();
    }

    public static void deletePlan(final Context context, final Plan plan, final RequestCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final int ret = PlanDao.deletePlan(context, plan);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            if (ret < 0) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess(plan);
                            }
                        }
                    }
                });
            }
        }).start();
    }

    public static void getPlan(final Context context, final int id, final RequestCallback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                final Plan plan = PlanDao.getPlan(context, id);
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        if (callback != null) {
                            if (plan == null) {
                                callback.onFailure("error");
                            } else {
                                callback.onSuccess(plan);
                            }
                        }
                    }
                });
            }
        }).start();
    }
}
